import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Product from '@/components/Product'
import Order from '@/components/Order'
import User from '@/components/User'
import Seckill from '@/components/Seckill'
import Recycle from '@/components/Recycle'
import ProductCateGory from '@/components/ProductCateGory'
import Login from '@/components/Login'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
import OrderInfo from '@/components/OrderInfo'
import Specification from '@/components/Specification'
import ShowSpecif from '@/components/ShowSpecif'


Vue.use(Router)
const routes=[
{
  path:'/',
  name:'Login',
  component:Login
},
{
  path: '/Product',
  name: 'Product',
  component: Product
},
{
  path: '/Order',
  name: 'Order',
  component: Order
},
{
  path:'/User',
  name:'User',
  component:User
},
{
  path:'/Seckill',
  name:'Seckill',
  component:Seckill
},
{
  path:'/Recycle',
  name:'Recycle',
  component:Recycle
},
{
  path:'/ProductCateGory',
  name:'ProductCateGory',
  component:ProductCateGory
},
{
  path:'/Specification/:id',
  name:'Specification',
  component:Specification
},
{
  path:'/OrderInfo/:id',
  name:'OrderInfo',
  component:OrderInfo
},
{
  path:'/ShowSpecif/:id',
  name:'ShowSpecif',
  component:ShowSpecif
}
];
  const router=new VueRouter({
    routes
  });
  //拦截路由守卫
  router.beforeEach((to,from,next)=>{
    console.log("进入拦截守卫Form",from);
    console.log("进入拦截守卫To",to.path);
    //to表示将要访问的路径
    //form代表从那个路径跳转而来
    //next代表一个函数  放行
    const tokenStr=window.sessionStorage.getItem('token');//获取token
    console.log("Token字符串",tokenStr);
    if(to.path==='/')return next();
    if(!tokenStr)
    {
      console.log("已拦截");
      ElementUI.Message.error("请先登录");
      return next("/");
    }
    console.log("11");
    next();  //存在就放行
  })
   export default router
